package com.xiyuan.almighty.service;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class TestStream {
    public static void main(String[] args) {
        List<Employee> dbs = Arrays.asList(new Employee("0Huxz", 45, "男"),
                new Employee("Yangdd", 31, "男"),
                new Employee("Suns", 33, "男"),
                new Employee("Liucy", 23, "男")
        );
//        Comparator<Employee> comparator = Comparator.comparing((Function<Employee, String>) e -> e.getName());
//        dbs.sort(comparator);
        dbs.sort(Comparator.comparing(Employee::getName));
        dbs.forEach(System.out::println);


        List<String> collect = dbs.stream().filter(e -> e.getAge() > 32).map(Employee::getName).collect(Collectors.toList());
        collect.forEach(System.out::println);
        String s = collect.get(0);
        boolean digit = Character.isDigit(s.charAt(0));
        System.out.println("digit = " + digit);

        List<String> list1 = Arrays.asList("ABC", "DEF", "XYZ", "ADG", "BCR", "XYA");
        Stream<String> stream = list1.stream().flatMap((String s1) -> {
            String[] ss = s1.split("");
            return Arrays.stream(ss);
        });
//        stream.distinct().collect(Collectors.toList()).forEach(System.out::println);


        List<String> list = new ArrayList<>();

        list.add("abc");
        list.add("ab");
        list.add("abcd");
        list.add("abcde");

        // 获取最大值
        int max = list.stream().map((s2) -> s2.length()).max(Integer::compareTo).get();
        System.out.println(max);

        // 获取最小值，另一种方法
        int min = list.stream().min(Comparator.comparing(s2 -> s2.length())).get().length();
        System.out.println(min);

        List<Integer> list3 = Stream.of("abc", "b", "a", "abcd").filter((s3) -> s3.startsWith("a")).map((s3) -> s3.length())
                .collect(Collectors.toList());
        System.out.println(list3);

        Employee employee = new Employee("张三", 28, "男");
        Optional<Employee> empOpt = Optional.of(employee);
        Employee employee1 = empOpt.orElse(null);
        System.out.println("employee1 = " + employee1);


        Employee employee2 = null;
        Optional<Employee> optional = Optional.ofNullable(employee2);
        Employee employee3 = optional.orElse(new Employee("111", 1, "女"));
        System.out.println(employee3);


        Optional opt = Optional.ofNullable(null);
        Optional opt1 = Optional.empty();

//        System.out.println(opt.isPresent());
//        System.out.println(opt1);

        if (empOpt.isPresent()) {
//            System.out.println(empOpt.get());
        }

//        empOpt.ifPresent(ss -> ss.getAge());

//        System.out.println(empOpt.get());

        System.out.println(optional.orElse(new Employee("张三1", 28, "男")));

        System.out.println(empOpt.orElseGet(() -> {
            return new Employee();
        }));
        System.out.println(optional.orElseGet(Employee::new));

        Optional<String> name = Optional.of("Sanaulla");
        Optional<String> upperName = name.map((value) -> value.toUpperCase());
        System.out.println(upperName.orElse("No value found"));

        Optional ooo = Optional.of(new Employee("Suns", 35, "男"));

        ooo.ifPresent(e -> System.out.println(e));

//        ooo.map();

    }
}

class Employee {
    private String name;
    private Integer age;
    private String gender;

    public Employee(String name, Integer age, String gender) {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "MysqlDB{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", gender='" + gender + '\'' +
                '}';
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Employee() {
    }
}
